//
//  BigImageViewer.m
//  WindRunner
//
//  Created by leeqb on 1/15/15.
//  Copyright (c) 2015 poweropt. All rights reserved.
//

#import "BigImageViewer.h"

@implementation BigImageViewer

- (id)init
{
    self = [super init];
    if(self) {
        CGRect rect = CGRectZero;
        rect.size.width = SCREEN_WIDTH;
        rect.size.height = SCREEN_HEIGHT;
        _imgScrollView = [[UIScrollView alloc] initWithFrame:rect];
        _imgScrollView.delegate = self;
        _imgScrollView.pagingEnabled = YES;
        _imgScrollView.backgroundColor = [UIColor blackColor];
        
        rect.origin.x = 10;
        rect.size.width = SCREEN_WIDTH - rect.origin.x*2;
        rect.size.height = 44;
        rect.origin.y = SCREEN_HEIGHT - rect.size.height;
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide:)];
        [_imgScrollView addGestureRecognizer:tapGesture];
        
        _pageLabel = [[UILabel alloc] initWithFrame:rect];
        _pageLabel.font = [UIFont boldSystemFontOfSize:18];
        _pageLabel.textAlignment = NSTextAlignmentRight;
        _pageLabel.backgroundColor = [UIColor clearColor];
        _pageLabel.textColor = [UIColor whiteColor];
    }
    return self;
}

- (void)hide:(UITapGestureRecognizer *)tapGesture
{
    [_pageLabel removeFromSuperview];
    [_imgScrollView removeFromSuperview];
    _pageLabel = nil;
    _imgScrollView = nil;
}

- (void)show
{
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview:_imgScrollView];
    [keyWindow addSubview:_pageLabel];
    
    CGRect rect = CGRectZero;
    rect.origin.y = (SCREEN_HEIGHT - SCREEN_WIDTH) / 2;
    rect.size.width = SCREEN_WIDTH;
    rect.size.height = SCREEN_WIDTH;
    for(int i=0; i<[_picArray count]; i++) {
        id picObj = _picArray[i];
        UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:rect];
        tempImageView.image = [UIImage getDownloadDefaultImage:CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH)];
        [_imgScrollView addSubview:tempImageView];
        if([picObj isKindOfClass:[NSDictionary class]]) {
            LEDownloadManager *downloadManager = [[LEDownloadManager alloc] initWithTarget:tempImageView];
            [downloadManager downLoadImageWithURL:[NSString formatString:picObj[@"url"]] imagetype:_catchDirectory];
        } else if([picObj isKindOfClass:[UIImage class]]) {
            tempImageView.image = picObj;
        }
        rect.origin.x += SCREEN_WIDTH;
    }
    
    _currIndex += 1;
    _pageLabel.text = [NSString stringWithFormat:@"%@/%@", @(_currIndex), @([_picArray count])];
    _imgScrollView.contentSize = CGSizeMake(rect.origin.x, SCREEN_HEIGHT);
    [_imgScrollView setContentOffset:CGPointMake((_currIndex-1)*SCREEN_WIDTH, 0)];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int page = _imgScrollView.contentOffset.x/_imgScrollView.frame.size.width + 1;
    if(page==0) page=1;
    _pageLabel.text = [NSString stringWithFormat:@"%@/%@", @(page), @([_picArray count])];
}

@end
